﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SMP_QuanLySV.DAL;
using SMP_QuanLySV.BLL;

namespace SMP_QuanLySV.Presentation
{
    public partial class UC_TrinhDo : UserControl
    {
        Form parent;
        BLLTrinhDo BLL = new BLLTrinhDo();
        int selectedIndex = -1;
        TrinhDo selectedInstance = null;
        string trangThai = "";

        // constructor
        public UC_TrinhDo(Form parent)
        {
            InitializeComponent();
            this.parent = parent;
        }

        void setDefaultSelectedInstance()
        {
            selectedIndex = 0;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            // update du lieu cho phu hop voi selected row
            updateTextboxes(selectedInstance);
        }

        private void UC_QLTrinhDo_Load(object sender, EventArgs e)
        {
            // nap du lieu cho datagridview
            dataGridViewX1.DataSource = BLL.returnListTrinhDo();

            // mac dinh dong dau tien trong datagridview duoc chon (selected)
            setDefaultSelectedInstance();

            setReadOnlyAll(true);
            
        }

        // update cac textboxes cho phu hop voi selected row trong datagridview
        void updateTextboxes(TrinhDo temp)
        {
            txtTrinhDoID.Text = temp.TrinhDoID;
            txtTenTrinhDo.Text = temp.TenTrinhDo;
            txtGhiChu.Text = temp.GhiChu;
        }

        // set thuoc tinh readOnly/enabled cho tat ca cac controls ve 1 gia tri
        void setReadOnlyAll(bool val)
        {
            txtTrinhDoID.ReadOnly = val;
            txtTenTrinhDo.ReadOnly = val;
            txtGhiChu.ReadOnly = val;
        }

        // cho thuoc tinh text cua cac textboxes tro thanh ""
        private void clearTextboxes()
        {
            txtTrinhDoID.Text = "";
            txtTenTrinhDo.Text = "";
            txtGhiChu.Text = "";
        }

        // xy ly su kien click chon 1 cell khac trong datagridview
        // --> lay row cua cell do lam selectedIndex va SelectedInstance bang row dc chon
        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            selectedIndex = dataGridViewX1.SelectedCells[0].RowIndex;
            selectedInstance = BLL.retrieveByIndex(selectedIndex);
            updateTextboxes(selectedInstance);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtTrinhDoID.Text == "")
            {
                MessageBox.Show("Bạn phải chọn trình độ nào cần xóa", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                DialogResult result = MessageBox.Show("Bạn có chắc muốn xóa hay không?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (result == DialogResult.Yes)
                {
                    // xoa doi tuong va update lai view
                    string TrinhDoIDCanDelete = txtTrinhDoID.Text;
                    TrinhDo TrinhDoCanDelete = BLL.retrieveByID(TrinhDoIDCanDelete);
                    BLL.deleteTrinhDo(TrinhDoCanDelete);

                    dataGridViewX1.DataSource = BLL.returnListTrinhDo();
                    setDefaultSelectedInstance();
                }
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            setReadOnlyAll(false);
            txtTrinhDoID.ReadOnly = true;

            // set trang thai ve che do sua
            trangThai = "dang_sua";
            setBtnEnabled1(false);
            setBtnEnabled2(true);
            dataGridViewX1.Enabled = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            // disable toan bo cac textboxes
            setReadOnlyAll(true);

            // kiem tra tinh hop le cua du lieu nhap vao
            TrinhDo dt = new TrinhDo();
            dt.TrinhDoID = txtTrinhDoID.Text;
            dt.TenTrinhDo = txtTenTrinhDo.Text;
            dt.GhiChu = txtGhiChu.Text;
            
            if (BLL.kiemTra(dt))
            {
                // update csdl va view
                if (trangThai == "dang_sua")
                {
                    BLL.updateTrinhDo(dt);
                }
                if (trangThai == "dang_them")
                {
                    BLL.insertTrinhDo(dt);
                }
                dataGridViewX1.DataSource = BLL.returnListTrinhDo();
                dataGridViewX1.Enabled = true;
                // disable cac nut Luu va Huy
                setBtnEnabled2(false);
                setBtnEnabled1(true);
                setDefaultSelectedInstance();
            }
            else
            {
                MessageBox.Show("Bạn nhập sai thông tin", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                btnHuy_Click(sender, e);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            // update lai view va buttons
            setDefaultSelectedInstance();
            setReadOnlyAll(true);
            dataGridViewX1.DataSource = BLL.returnListTrinhDo();
            setBtnEnabled2(false);
            setBtnEnabled1(true);

            dataGridViewX1.Enabled = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            clearTextboxes();
            setReadOnlyAll(false);
            setBtnEnabled2(true);
            setBtnEnabled1(false);

            // set trang thai ve che do them moi
            trangThai = "dang_them";
            dataGridViewX1.Enabled = false;
        }

        void setBtnEnabled1(bool val)
        {
            btnThem.Enabled = val;
            btnXoa.Enabled = val;
            btnSua.Enabled = val;
        }

        void setBtnEnabled2(bool val)
        {
            btnLuu.Enabled = val;
            btnHuy.Enabled = val;
        }
    }
}
